/**
 * Created by Administrator on 2018/11/6 0006.
 */
let Promise = require('bluebird');
const utils = require('../../public/utils');
const TokenModel = require('../../model/token-model');

const obj = {
    /*
    * 根据token获取userId
    * */
    getUserIdByToken(token) {
        return new Promise((resolve,reject) => {
            if(!utils.checkObjectId(token)) {
                resolve('');
                return;
            }
            TokenModel.findOne({_id:token},(err,data) => {
                if(err) {
                    resolve(9999);
                    return;
                }
                if(data) {
                    resolve(data.userId);
                }else {
                    resolve('');
                }
            })
        })
    },
    /*
    * 删除某个token
    * */
    deleteOneToken(token) {
        return new Promise((resolve,reject) => {
            if(!utils.checkObjectId(token)) {
                resolve(true);
                return;
            }
            TokenModel.findOne({_id:token},(err,data) => {
                if(err) {
                    resolve(9999);
                    return;
                }
                if(data) {
                    data.remove();
                    resolve(true);
                }else {
                    resolve(true);
                }
            })
        })
    },
    /*
    * 根据userId获取token
    * */
    getTokenByUserId(userId) {
        return new Promise((resolve,reject) => {
            TokenModel.findOne({userId},(err,data) => {
                if(err) {
                    reject(err);
                    return;
                }
                if(data) {
                    resolve(data._id);
                }else {
                    resolve('');
                }
            })
        })
    },
    /*
    * 性别code转为text
    * */
    changeSexCodeToText(code) {
        let dict = ['','男','女','未知'];
        return dict[code];
    }

}
module.exports = obj;